package cn.itcast.bos.web.action.courier;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.dom4j.CDATA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.courier.CourierService;
import cn.itcast.crm.domain.Customer;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
public class CourierAction extends ActionSupport implements ModelDriven<Courier>{
	@Autowired
	private CourierService courierService;
	private Courier courier= new Courier();
	
	public Courier getModel() {
		
		return courier;
	}
	
	private String fixedAreaId;
	
	public void setFixedAreaId(String fixedAreaId) {
		this.fixedAreaId = fixedAreaId;
	}

	//查找没有被关联的客户 在定区关联客户的列表中显示
	@Action(value="findNoLinkedCourier",results={@Result(name="success",type="json")})
	public String findNoLinkedCustomer(){
		List<Courier>list=courierService.findNoLinkedCourier(fixedAreaId);
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	
	//查询与指定定区线关联的客户
	
	private String fixedid;
	
	public void setFixedid(String fixedid) {
		this.fixedid = fixedid;
	}

	@Action(value="findLinkedCourier",results={@Result(name="success",type="json")})
	public String findLinkedCourier(){
		List<Courier>list=courierService.findLinkCourier(fixedid);
		
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	
	
	
	
	// 逻辑删除 作废 
	private String ids;	
	public void setIds(String ids) {
		this.ids = ids;
	}

	@Action(value="courier_delete",results={@Result(name="success",
			location="/pages/base/courier.html" ,type="redirect")})
	public String delete(){
		courierService.delete(ids);
		
		return SUCCESS;
	}
	

	@Action(value="courier_restore",results={@Result(name="success",
			location="/pages/base/courier.html" ,type="redirect")})
	public String restore(){
		courierService.restore(ids);
		return "success";
	}
	
	
	//分页查询功能的实现
	private int page;
	private int rows;	
	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	@Action(value="courier_pageQuery",results={@Result(name="success",type="json")})	
	public String pageQuery(){
		//封装数据
		Pageable pageable= new PageRequest(page-1, rows);
	
		//封装条件查询的对象Specification
		Specification<Courier>specification= new Specification<Courier>() {
			
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				/**
				 * root 就代表courier 可以生成简单的表达式
				 * query 可以生成简单的条件  query.where(表达式)
				 * cd 构建复杂的条件
				 */
				
				List<Predicate>list= new ArrayList<Predicate>();
				//判断传过来的数据courierNum是否为空 " "  "" null
				if(StringUtils.isNotBlank(courier.getCourierNum())){
				Predicate p1=cb.equal(root.get("courierNum").as(String.class),
						courier.getCourierNum());
				list.add(p1);
				}
				if(StringUtils.isNotBlank(courier.getCompany())){
					// company like % %
					Predicate p2=cb.like(root.get("company").as(String.class),
							"%"+courier.getCompany()+"%");
					list.add(p2);
				}
				if(StringUtils.isNotBlank(courier.getType())){
					//root.get(String) 得到的是一个字段名     后面得到的是字段的值
					Predicate p3=cb.equal(root.get("type").as(String.class), 
							courier.getType());
					list.add(p3);
				}
				//多表的查询
				Join<Courier,Standard>standardJoin=root.join("standard",JoinType.INNER);
				if(courier.getStandard()!=null&&StringUtils.isNotBlank(courier.getStandard().getName())){
					Predicate p4=cb.like(standardJoin.get("name").as(String.class),
							"%"+courier.getStandard().getName()+"%");
					list.add(p4);
				}			
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		
		// 调用方法
		Page<Courier>pageData=courierService.findPageData(specification,pageable);
		
		// 将返回page对象 转换datagrid需要格式
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", pageData.getTotalElements());
		result.put("rows", pageData.getContent());
		// 将结果对象 压入值栈顶部
		ActionContext.getContext().getValueStack().push(result);		
		return "success";
	}
	
	@Action(value="courier_save",results={@Result(name="success",
			location="/pages/base/courier.html" ,type="redirect")})
	public String save(){
		courierService.save(courier);
		return "success";
	}
	
}
